const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    imgSrc: SERVICE.imgUrl,
    sloganList: null,
    inputFocus: false,
    text: "",
    listItem: null
  },
  //分享 
  onShareAppMessage(){},

  onLoad(options){
    let t = options.type, id = options.id;
    if(t == 0){
      this.shareRentImage(id);
      this.setData({
        title: "编辑租赁广告"
      });
    }else if(t == 1){
      this.shareRecruitImage(id);
      this.setData({
        title: "编辑招聘广告"
      });
    }
  },
  input(){
    let td = this.data;
    this.setData({
      inputFocus: !td.inputFocus
    });
  },
  // blurInput(){
  //   this.setData({
  //     inputFocus: false
  //   });
  // },
  list(e){
    let td = this.data, index = e.currentTarget.dataset.index;
    this.setData({
      text: td.sloganList[index],
      listItem: index,
      inputFocus: false
    });
  },
  copy(e){
    let input_val = e.detail.value.input;
    if (input_val.length == 0){
      wx.showToast({
        title: "请输入复制内容",
        icon: "none"
      });
    }else{
      wx.setClipboardData({
        data: input_val,
        success: function (res) {
          wx.getClipboardData({
            success: function (res) {
              console.log(res.data) // data
            }
          })
        }
      })
    }
  },

  saveImg(e){
    let imgSrc = e.currentTarget.dataset.src;
    console.log(imgSrc);
    if (imgSrc){
      wx.showLoading({
        title: "生成图片中..",
        mask: true
      });
      wx.downloadFile({
        url: imgSrc,
        success: function(res){
          wx.hideLoading();
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: function (res) {
              wx.showToast({
                title: "保存成功",
                icon: "none"
              });
            },
            fail: function (res) {
              wx.showToast({
                title: "保存失败",
                icon: "none"
              });
            }
          })
        }
      })
    }
  },

  // 租赁
  shareRentImage(id){
    let _this = this, td = this.data;
    SERVICE.POST(SERVICE.URL.shareRentImage, {
      rent_id: id
    }, (status, res) => {
      console.log(res);
      _this.setData({
        img: td.imgSrc + res.data.src,
        sloganList: res.data.sloganList
      });
    });
  },

  // 招聘
  shareRecruitImage(id) {
    let _this = this, td = this.data;
    SERVICE.POST(SERVICE.URL.shareRecruitImage, {
      recruitment_id: id
    }, (status, res) => {
      _this.setData({
        img: td.imgSrc + res.data.src,
        sloganList: res.data.sloganList
      });
    });
  }
});